set (CC_COMMS_EXTERNAL_PROJ_TGT "cc_comms_external")
set (CC_COMMS_EXTERNAL_PROJ_REPO "https://github.com/commschamp/comms.git")
set (CC_COMMS_EXTERNAL_PROJ_TAG "v4.0")

set (cxx_standard_opt)
if (NOT "${CMAKE_CXX_STANDARD}" STREQUAL "")
    set (cxx_standard_opt -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD})
endif ()

include(ExternalProject)
ExternalProject_Add(
    "${CC_COMMS_EXTERNAL_PROJ_TGT}"
    PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
    GIT_REPOSITORY "${CC_COMMS_EXTERNAL_PROJ_REPO}"
    GIT_TAG ${CC_COMMS_EXTERNAL_PROJ_TAG}
    SOURCE_DIR "${CC_COMMS_EXTERNAL_PROJ_SRC_DIR}"
    BINARY_DIR "${CC_COMMS_EXTERNAL_PROJ_BUILD_DIR}"
    INSTALL_DIR "${CC_COMMS_EXTERNAL_PROJ_INSTALL_DIR}"
    CMAKE_ARGS 
        -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} 
        -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
        -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
        -DCMAKE_GENERATOR=${CMAKE_GENERATOR}
        -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
        -DCMAKE_INSTALL_PREFIX=${CC_COMMS_EXTERNAL_PROJ_INSTALL_DIR}
        -DCC_COMMS_BUILD_UNIT_TESTS=${CC_BUILD_UNIT_TESTS}
        -DCC_COMMS_UNIT_TESTS_USE_SANITIZERS=${CC_UNIT_TESTS_USE_SANITIZERS}
        -DCC_COMMS_WARN_AS_ERR=${CC_WARN_AS_ERR}
        -DCC_COMMS_USE_CCACHE=${CC_USE_CCACHE}
        -DCC_COMMS_SKIP_CXX_STANDARD_FORCING=${CC_SKIP_CXX_STANDARD_FORCING}
        -DCC_COMMS_STATIC_RUNTIME=${CC_STATIC_RUNTIME}
        -DCC_COMMS_EXTERNALS_DIR=${CC_EXTERNALS_DIR}
        ${cxx_standard_opt}
)

set (comms_inc_dir "${CC_COMMS_EXTERNAL_PROJ_INSTALL_DIR}/include")
execute_process(
    COMMAND ${CMAKE_COMMAND} -E make_directory ${comms_inc_dir}
)    

add_library(cc::comms INTERFACE IMPORTED GLOBAL)
target_include_directories(cc::comms INTERFACE ${comms_inc_dir})
add_dependencies(cc::comms ${CC_COMMS_EXTERNAL_PROJ_TGT})

install(
    FILES dummy.txt
    DESTINATION ${CMAKE_CURRENT_BINARY_DIR}
)

find_package (Doxygen)
if (DOXYGEN_FOUND)
    add_custom_target ("doc_comms"
        COMMAND ${CMAKE_COMMAND} 
            --build ${CC_COMMS_EXTERNAL_PROJ_BUILD_DIR} --target doc_comms
        DEPENDS ${CC_COMMS_EXTERNAL_PROJ_TGT}
    )
endif ()
